# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
# a = set(['a','b','c'])
# b = set(['a','b'])
# print(a-b)
# a = ['a','b']
# b = ['c','d']
# c = ['e','f']
# d = [a, b, c]
# e = []
# for i in a:
#     for j in b:
#         for k in c:
#             e.append(i+j+k)
# for i in d:
#     for j in i:
#         e.append(j)
#
# import itertools
# print(list(itertools.permutations(e,3)))
# print(e)
# from functools import reduce
# def myfunc(lists):
#     # list all possible composition from many list, each item is a tuple.
#     # Here lists is [list1, list2, list3], return a list of [(item1,item2,item3),...]
#     # len of result list and result list.
#     total = reduce(lambda x, y: x * y, map(len, lists))
#     retList = []
#     # every item of result list.
#     for i in range(0, total):
#         step = total
#         tempItem = []
#         for l in lists:
#             step /= len(l)
#             tempItem.append(l[int(i / step % len(l))])
#         retList.append('.'.join(tempItem))
#     return retList
#
# xxx = myfunc(d)
# print(xxx)
# import json
# str = '''["S.QG16.GEN.PE.MT", "S.QG16.GEN.PE.AT", "S.QG16.GEN.SE.MT", "S.QG16.GEN.SE.AT", "S.QG16.GCC.PE.MT", "S.QG16.GCC.PE.AT", "S.QG16.GCC.SE.MT", "S.QG16.GCC.SE.AT", "QG15"]'''
# l = json.loads(str)
# vin_l = ["S","QG16","MT","PE","GCC"]
# vin_set = set(vin_l)
# i = False
# for i in l:
#     part_l = i.split('.')
#     print(part_l)
#     car_set = set(part_l)
#     res = car_set-vin_set
#     if len(res) == 0:
#         print(car_set)
#         i = True
#         break
# print(i)
# a = {'a':'a', 'b':'b'}
# b = {'b':'b', 'a':'a'}
# print(a==b)


# vinyears = '20180808'
# caryears = '20170808-20190808'
# caryears1 = '20180808-20190808'
# caryears2 = '20180809-20190808'
# caryears3 = '20180809-'
# caryears4 = '20170809-20170909'
#
# def compare_date(vinyears, caryears):
#     assert isinstance(vinyears, str) and len(vinyears) == 8, u'日期格式不正确'
#     res = False
#     vinyears = int(vinyears)
#     caryears_str = caryears.split("-")
#     try:
#         caryears = [int(i) if i else 99999999 for i in caryears_str]
#     except:
#         raise Exception(u"日期格式不正确")
#     if len(caryears) == 1:
#         caryears[1] = 99999999
#     if caryears[0] <= vinyears and caryears[1] >= vinyears:
#         res = True
#     return res
#
# print(compare_date(vinyears, caryears4))

# a = ('a'+'.'+'b'+'.'+''+'.'+''+'.'+''+'.'+''+'.'+'').replace("..", ".").replace("..",".").replace("..",".")

import json
#
# extraKey = '[{"F11": "", "F4": "4S", "engine": "VQ35DE", "trans": "CVT", "driver": "", "grade": "35LE", "area": "MEXICO", "years": "20120600"},{"F11": "", "F4": "4S", "engine": "VQ35DE", "trans": "CVT", "driver": "", "grade": "PREMIUM", "area": "MEXICO", "years": "20120600"}]'
#
# vin_extra_info = [{"years": i.get("years"), "sa": (
#             i.get("F4") + '.' + i.get("F11") + '.' + i.get("area") + '.' + i.get("trans") + '.' + i.get(
#         "driver") + '.' + i.get("grade") + '.' + i.get("engine")).replace("..", ".").replace("..", ".").replace("..",
#                                                                                                                 ".")}
#                   for i in json.loads(extraKey)]
# print(vin_extra_info)

# def compare_sa(vinsa, carsa):
#     vinsa_l = vinsa.split(".")
#     carsa_l = carsa.split(".")
#     vset = set(vinsa_l)
#     cset = set(carsa_l)
#     return not cset-vset
# vinsa = "4S.MEXICO.CVT.35LE.VQ35DE"
# vinsa = "4S.MEXICO.CVT.PREMIUM.VQ35DE"
# carsa = "VQ35DE"
#
# print(compare_sa(vinsa, carsa))
#
# a = '''{"extra_info":{"extra_info": [{"years": "20090700-20110800", "F9": "", "F8": "01", "F10": "", "F11": "", "F12": "", "sa": ["VQ35DE"], "F14": "", "F15": "", "F16": "", "F17": ""}, {"years": "20110800-", "F9": "", "F8": "01", "F10": "", "F11": "", "F12": "", "sa": ["VQ35DE"], "F14": "", "F15": "", "F16": "", "F17": ""}, {"years": "20080500-20090700", "F9": "", "F8": "01", "F10": "", "F11": "", "F12": "", "sa": ["VQ35DE"], "F14": "", "F15": "", "F16": "", "F17": ""}], "qty": "01", "rid": "", "imgPoints": [{"x": "1896.36", "y": "376.38"}]}}'''
# _d = json.loads(a)
# ex = _d.get("extra_info").get("extra_info")
# print(ex[0]['years'])

# def compare_sa(vinsa, carsa):
#     # vinsa: ["",""]
#     # carsa: ["",""]  可能是ALL
#     if not vinsa or not carsa:
#         return True
#     for csa in carsa:
#         for vsa in vinsa:
#             # 先去除*, *代表可选配件,不是过滤条件
#             csa = csa.replace("*", "")
#             vsa = vsa.replace("*", "")
#             if csa == "ALL":  # 如果零件过滤条件是all,直接通过
#                 return True
#             vinsa_l = vsa.split(".")
#             carsa_l = csa.split(".")
#             vset = set(vinsa_l)
#             cset = set(carsa_l)
#             if not cset - vset:
#                 return True
#     return False
#
# vinsa = ["4S.MEXICO.CVT.35LE.VQ35DE", "4S.MEXICO.MVT.35LE.VQ35DE"]
# vinsa = ["4S.MEXICO.CVT.PREMIUM.VQ35DE"]
# carsa = ["VQ35DE.AREA", "CVT"]
# print(compare_sa([],[]))

# a = {"a":"a", "b":"c"}
# a.pop('a')
# print(a)
# print(a.values())

# a = set(["a","b"])
# b = set(["a","b","c","d"])
# c = a-b
# print(True if not c else False)

# def fn(arg):
#     return arg and arg.strip()
#
# l = ['','x',' ']
# dl = filter(fn, l)
# print(list(dl))

# def no_odd():
#     i = 3
#     while True:
#         yield i
#         i+=2
# def _filter(n):
#     return lambda x: x % n > 0
#
# def res(end):
#     yield 2
#     it = no_odd()
#     while True:
#         n = next(it)
#         if n > end:
#             break
#         yield n
#         it = filter(_filter(n), it)

# for i in res(50):
#     print(i)

# list = [36, 5, -12, 9, -21]
# res = sorted(list, key=abs)
# print(res)
# from mytools import lists_combination
# import re
# str = 'a*(b/c/g)*d*(e/f)/a*g'
#
# def fn(str):
#     str = str.replace('*',"")
#     kuohao = re.findall('\(.*?\)',str)
#     _l = []
#     for i in kuohao:
#         str = str.replace(i, '')
#         _l.append(i.replace('(','').replace(')','').split('/'))
#     for i in str:
#         _l.append([i])
#     # print(_l)
#     return lists_combination(_l, '&')
#
# def fn2(str):
#     kuohao = re.findall('\(.*?\)',str)
#     for i in kuohao:
#         j = i.replace("/","+")
#         str = str.replace(i, j)
#     str_list = str.split('/')
#     ret = []
#     for i in str_list:
#         i = i.replace('+','/')
#         res = fn(i)
#         ret.extend(res)
#     return ret

# print(fn2(str))

# import pymongo
# mongocli = pymongo.MongoClient(host='localhost', port=27017)
# mongodb = mongocli['nissan']
# mongocol = mongodb['cars_group_collection']
# mongocol.insert({"a":"a"})

# import hashlib
#
#
# def get_md5(str):
#     '''获取md5加密电子指纹'''
#     md5 = hashlib.md5()
#     md5.update(str)
#     return md5.hexdigest()

# with open('/Users/duanmengdi/Desktop/nissan_group_img/0938fc2e5d8913bb61f39ec7322a613e.png', 'rb') as f:
#     img_data = f.read()
# img_md5 = get_md5(img_data)
# print(img_md5)

# with open()
from functools import reduce
years_list = ['20180808-20180909', '20170707-20180808', '20160808-20170707', '20190808-20190809']
years_l = [[int(j) if j else 99999999 for j in i.split("-")] for i in years_list]

years_l = [int(''.join([j if j else '99999999' for j in i.split("-")])) for i in years_list]
years_l.sort()
print(years_l)
res_l = []
for i in range(len(years_l)-1):
    print(i, i+1)
    j = i+1
    y1 = years_l[i]
    y2 = years_l[j]
    y1_start = int(str(y1)[:8])
    y1_end = int(str(y1)[8:])
    y2_strart = int(str(y2)[:8])
    y2_end = int(str(y2)[8:])
    if y1_end == y2_strart:
        res = str(y1_start)+'-'+str(y2_end)
        res_l.append(res)
    else:
        y1 = str(y1_start)+'-'+str(y1_end)
        y2 = str(y2_strart)+'-'+str(y2_end)
        if j == len(years_l)-1:
            res_l.append(y1)
            res_l.append(y2)
        else:
            res_l.append(y1)
print(res_l)










